package com.joymart.qualification.rest;


import com.joymart.qualification.model.QualificationApplication;
import com.joymart.qualification.service.ApplicationService;
import com.joymart.user.model.BusinessRole;
import lombok.Data;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class QualificationController {

    @Autowired
    private ApplicationService applicationService;


    @PostMapping("/shop/applications")
    public Object apply(@RequestBody ShopApplicationParams params) {
        QualificationApplication application = applicationService.createApplication(params.toQualificationApplication());
        return applicationService.toDto(application);
    }

    @PostMapping("/whs/applications")
    public Object apply(@RequestBody WhsApplicationParams params) {
        QualificationApplication application = applicationService.createApplication(params.toQualificationApplication());
        return applicationService.toDto(application);
    }

    @PostMapping("/agent/applications")
    public Object apply(@RequestBody AgentApplicationParams params) {
        QualificationApplication application = applicationService.createApplication(params.toQualificationApplication());
        return applicationService.toDto(application);
    }

    @PostMapping("/manager/applications")
    public Object apply(@RequestBody ManagerApplicationParams params) {
        QualificationApplication application = applicationService.createApplication(params.toQualificationApplication());
        return applicationService.toDto(application);
    }

    @Data
    private static class ManagerApplicationParams {
        //签名图片地址
        private String signatureUrl;
        private String protocolId;
        private String evaluationId;
        // 合同附件
        private String contract;

        public QualificationApplication toQualificationApplication() {
            QualificationApplication application = new QualificationApplication();
            application.setSignatureUrl(signatureUrl);
            application.setProtocolId(protocolId);
            application.setTargetRole(BusinessRole.MANAGER);
            application.setMeta(new JSONObject());
            application.getMeta().put("evaluationId", evaluationId)
            .put("contract", contract);
            return application;
        }
    }
    @Data
    private static class AgentApplicationParams {
        //签名图片地址
        private String signatureUrl;
        private String protocolId;
        private String evaluationId;

        public QualificationApplication toQualificationApplication() {
            QualificationApplication application = new QualificationApplication();
            application.setSignatureUrl(signatureUrl);
            application.setProtocolId(protocolId);
            application.setTargetRole(BusinessRole.AG_AGENT);
            application.setMeta(new JSONObject());
            application.getMeta().put("evaluationId", evaluationId);
            return application;
        }
    }

    @Data
    private static class WhsApplicationParams {
        //签名图片地址
        private String signatureUrl;
        private String licenseUrl;
        private String protocolId;
        private String evaluationId;

        public QualificationApplication toQualificationApplication() {
            QualificationApplication application = new QualificationApplication();
            application.setSignatureUrl(signatureUrl);
            application.setProtocolId(protocolId);
            application.setTargetRole(BusinessRole.AG_WHS);
            application.setMeta(new JSONObject());
            application.getMeta().put("licenseUrl", licenseUrl);
            application.getMeta().put("evaluationId", evaluationId);
            return application;
        }
    }

    @Data
    private static class ShopApplicationParams {
        //签名图片地址
        private String signatureUrl;
        private String protocolId;

        public QualificationApplication toQualificationApplication() {
            QualificationApplication application = new QualificationApplication();
            application.setSignatureUrl(signatureUrl);
            application.setProtocolId(protocolId);
            application.setTargetRole(BusinessRole.AG_SHOP);
            application.setMeta(new JSONObject());
            return application;
        }
    }
}
